package com.bluetrum.abmate.dialog;

import android.app.Dialog;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import com.bluetrum.abmate.R;

public class DialogFragmentSingleChoice extends DialogFragment {

    private static final String CHOICE_TITLE = "CHOICE_TITLE";
    private static final String CHOICE_ITEMS = "CHOICE_ITEMS";
    private static final String CURRENT_CHOICE = "CURRENT_CHOICE";

    private String mTitle;
    private String[] mChoiceItems;
    private int mCurrentChoice;

    private DialogFragmentSingleChoiceListener mSingleChoiceListener;

    public static DialogFragmentSingleChoice newInstance(final String title, final String[] items, final int which) {
        DialogFragmentSingleChoice fragmentSingleChoice = new DialogFragmentSingleChoice();
        final Bundle args = new Bundle();
        args.putString(CHOICE_TITLE, title);
        args.putStringArray(CHOICE_ITEMS, items);
        args.putInt(CURRENT_CHOICE, which);
        fragmentSingleChoice.setArguments(args);
        return fragmentSingleChoice;
    }

    public static DialogFragmentSingleChoice newInstance(final String title, final String[] items, final int which,
                                                         final DialogFragmentSingleChoiceListener singleChoiceListener) {
        DialogFragmentSingleChoice fragmentSingleChoice = newInstance(title, items, which);
        fragmentSingleChoice.mSingleChoiceListener = singleChoiceListener;
        return fragmentSingleChoice;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle args = getArguments();
        if (args != null) {
            mTitle = args.getString(CHOICE_TITLE);
            mChoiceItems = args.getStringArray(CHOICE_ITEMS);
            mCurrentChoice = args.getInt(CURRENT_CHOICE);
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
        builder.setTitle(mTitle);

        builder.setSingleChoiceItems(mChoiceItems, mCurrentChoice, null);

        builder.setPositiveButton(R.string.ok, ((dialog, which) -> {
            final int index = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
            // 如果有设置Listener，则只调用Listener
            if (mSingleChoiceListener != null) {
                mSingleChoiceListener.onChooseItem(mChoiceItems[index], index);
            }
            // 如果ParentFragment实现了DialogFragmentSingleChoiceListener接口，则调用ParentFragment的
            else if (requireParentFragment() instanceof DialogFragmentSingleChoiceListener) {
                ((DialogFragmentSingleChoiceListener) requireParentFragment()).onChooseItem(mChoiceItems[index], index);
            }
            // 如果FragmentActivity实现了DialogFragmentSingleChoiceListener接口，则调用FragmentActivity的
            else if (requireActivity() instanceof DialogFragmentSingleChoiceListener) {
                ((DialogFragmentSingleChoiceListener) requireActivity()).onChooseItem(mChoiceItems[index], index);
            }
        }));
        builder.setNegativeButton(R.string.cancel, null);

        return builder.create();
    }

    public interface DialogFragmentSingleChoiceListener {
        void onChooseItem(String item, int index);
    }
}
